local canxu = fk.CreateSkill{
  name = "#plum__canxu_refresh",
}

Fk:loadTranslationTable{ [canxu.name] = "掺虚" }

local getCanxuMark = function (player)
  local mark = player:getMark("canxu_viewas-turn")
  return type(mark) == "table" and mark or nil
end

canxu:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    if player == target then
      return table.find(player.room:getAlivePlayers(false), function (p)
        if getCanxuMark(p) then
          return true
        end
      end)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers(false)) do
      local mark = getCanxuMark(p)
      if mark and mark[2] and table.contains(mark[2], data.card.trueName) then
        table.removeOne(mark[2], data.card.trueName)
        room:setPlayerMark(p, "canxu_viewas-turn", mark)
      end
    end
  end
})

canxu:addEffect(fk.TurnEnd, {
  can_refresh = function (self, event, target, player, data)
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers(false)) do
      room:handleAddLoseSkills(p, "-plum__canxu_viewas&")
    end
  end
})

return canxu
